package com.glut.gultguy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glut.gultguy.To.ArticleTo;
import com.glut.gultguy.entity.*;
import com.glut.gultguy.mapper.*;
import com.glut.gultguy.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glut.gultguy.utils.SnowflakeIdWorker;
import com.glut.gultguy.vo.ArticleVo;
import com.glut.gultguy.vo.commentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-05
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired(required = false)
    private ArticleMapper mapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private ImagesMapper imagesMapper;

    @Autowired(required = false)
    private CommentMapper commentMapper;

    @Autowired
    private SnowflakeIdWorker idWorker;
    @Autowired(required = false)
    private UsersLikeMapper likeMapper;

    @Override
    public List<ArticleVo> getArticle(Integer page) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("publishTime");
        queryWrapper.eq("_lock",false);
        Page<Article> page1 = new Page<>(page,20);
        page1 = mapper.selectPage(page1,queryWrapper);
        List<ArticleVo> articleVos = this.articlesRes(page1.getRecords());
       return articleVos;
    }

    @Override
    public Map<String,Integer> addArticle(ArticleTo to) {
     Article article = new Article();
     String id = idWorker.nextId()+"";
     article.set_id(id);
     article.set_openid(to.getPublisher().get_openid());
     article.set_lock(to.getLock());
     article.setClick(to.getClick());
     article.setContent(to.getContent());
     article.setPublishTime(to.getPublishFormatTime());
     int t = mapper.insert(article);
     for(String img : to.getImagesUrl()){
         Images images = new Images();
         images.set_id(idWorker.nextId()+"");
         images.setImagesUrl(img);
         images.setArticle_id(id);
         imagesMapper.insert(images);
     }
     Map<String ,Integer> map = new HashMap<>();
       if (t==1){
           map.put("code",200);
       }else {
           map.put("code",500);
       }
     return map;
    }

    @Override
    public List<ArticleVo> getMyArticles(String openid) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("_openid",openid);
        wrapper.orderByDesc("publishTime");
        List<Article> articles = this.baseMapper.selectList(wrapper);
        return this.articlesRes(articles);
    }

    @Override
    public ArticleVo getOneArticle(String articleId) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("_id",articleId);
        Article article =  this.baseMapper.selectOne(wrapper);
        return this.getArticleVo(article);
    }

    private List<ArticleVo> articlesRes(List<Article> articles){
        List<ArticleVo> articleVos = articles.stream().map(article -> {
        return this.getArticleVo(article);
        }).collect(Collectors.toList());
        return articleVos;
    }
    private ArticleVo getArticleVo(Article article){
        ArticleVo vo = new ArticleVo();
        BeanUtils.copyProperties(article,vo);
        vo.setPublisher(userMapper.selectOne(new QueryWrapper<User>().eq("_openid",vo.get_openid())));
        List<String> imageList =    imagesMapper.selectList(new QueryWrapper<Images>().eq("article_id",vo.get_id()))
                .stream().map(images -> {
                    return images.getImagesUrl();
                }).collect(Collectors.toList());
        vo.setImagesUrl(imageList);
        vo.setLock(article.get_lock());
        List<commentVo> commentVos = commentMapper.selectList(new QueryWrapper<Comment>().eq("article_id",vo.get_id()))
                .stream().map(comment -> {
                    commentVo theVo = new commentVo();
                    theVo.setCommentId(comment.get_id());
                    User user = userMapper.selectOne(new QueryWrapper<User>().eq("_openid",comment.get_openid()));
                    try {
                        if (comment.getParent_id() != null){
                            Comment parentId = commentMapper.selectOne(new QueryWrapper<Comment>().eq("_id",comment.getParent_id()));
                            User relay = userMapper.selectOne(new QueryWrapper<User>().eq("_openid",parentId.get_openid()));
                            theVo.setObjName(relay.getNickName());
                        }
                    }catch (Exception e){
                    }
                    theVo.setAvatarUrl(user.getAvatarUrl());
                    theVo.setUserName(user.getNickName());
                    theVo.set_openid(comment.get_openid());
                    theVo.setPublishTime(comment.getPublishTime());
                    theVo.setContent(comment.getComment());
                    return theVo;
                }).collect(Collectors.toList());
        vo.setComment(commentVos);
        List<String> likeList = likeMapper.selectList(new QueryWrapper<UsersLike>().eq("article_id",vo.get_id()))
                .stream().map(v -> {
                    return v.getUserId();
                }).collect(Collectors.toList());
        vo.setLike(likeList);
        return vo;
    }
}
